﻿using System.ComponentModel;
using System.Windows;
using FU.Capstones.IMS.Web.Common;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.DataContracts;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.EventArgs;
using FU.Capstones.IMS.Web.Silverlight.Models;
using Microsoft.Practices.Prism.Commands;


namespace FU.Capstones.IMS.Web.Silverlight.Popups
{
    public partial class AddNewContactPopup : INotifyPropertyChanged
    {
        public AddNewContactPopup( int partnerID)
        {
            InitializeComponent();
            DataContext = this;
            Contact = new Contact(){PartnerID = partnerID};
            
            model.InsertContactCompleted += AddCompleted;
        }

        ContactModel model = new ContactModel();


        #region SaveCommand

        private DelegateCommand<object> saveCommand;
        public DelegateCommand<object> SaveCommand
        {
            get
            {
                if (saveCommand == null)
                    saveCommand = new DelegateCommand<object>(DoSaveCommand);
                return saveCommand;
            }
        }

        private void DoSaveCommand(object obj)
        {
            Contact.ValidateAll();

            if (!Contact.HasErrors)
            {
                MessageVisibility = Visibility.Collapsed;
                model.AddNewContactAsync(contact);
                IsBusy = true;
            }
            else
            {
                Message = WebConstants.InvalidInputDataMessage;
                MessageVisibility = Visibility.Visible;
            }
        }

        #endregion

        #region CancelCommand

        private DelegateCommand<object> cancelCommand;
        public DelegateCommand<object> CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                    cancelCommand = new DelegateCommand<object>(DoCancelCommand);
                return cancelCommand;
            }
        }

        private void DoCancelCommand(object obj)
        {
            DialogResult = false;
        }

        #endregion

        #region AddCompleted

        void AddCompleted(object sender, SingleFeedbackCompletedEventArgs<int > e)
        {
            IsBusy = false;
            if (e.Error != null)
            {
                MessageVisibility = Visibility.Visible;
                Message = e.Error.Message;
            }
            else
            {
                if (e.Result.IsSuccessful)
                {
                    DialogResult = true;
                }
                else
                {
                    MessageVisibility = Visibility.Visible;
                    Message = e.Result.Message;
                }
            }

        }

        #endregion

        #region Binding Property
        private Contact contact;
        public Contact Contact
        {
            get { return contact; }
            set
            {
                if (!ReferenceEquals(contact, value))
                {
                    contact = value;
                    RaisePropertyChanged("Contact");
                }
            }
        }

        private Visibility messageVisibility;
        public Visibility MessageVisibility
        {
            get { return messageVisibility; }
            set
            {
                if (messageVisibility != value)
                {
                    messageVisibility = value;
                    RaisePropertyChanged("MessageVisibility");
                }
            }
        }

        private string message;
        public string Message
        {
            get { return message; }
            set
            {
                if (message != value)
                {
                    message = value;
                    RaisePropertyChanged("Message");
                }
            }
        }

        private bool isBusy;
        public bool IsBusy
        {
            get { return isBusy; }
            set
            {
                if (isBusy != value)
                {
                    isBusy = value;
                    RaisePropertyChanged("IsBusy");
                }
            }
        }

        #endregion

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}

